import { getWorkOrderList, assConfirm, assReject } from "@/apis/workOrder.js";
import { parseTime, getTotalPageNum } from "@/util";
import {
  getPartnerList,
  getEmployeeList,
} from "@/apis/common.js";

import {
  getRepairTypeList,
  getRepairCatgList,
  getBusiStatusList
} from "@/apis/workOrder.js";

import {
	deepClone
} from "@/util";


export default {
  data() {
    return {
      currStatus: 4,
      showOrderStatus: false,
      showAssStatus: false,
      showRepairType: false,
      showRepairCatg: false,
      showSalesman: false,
      showStartTime: false,
      showEndTime: false,
      showCustomer: false,
      showPicker: false,
      employeeParams: {
        PageSize: 10,
        PageNo: 1,
        DeptId: '',
        GroupId: '',
        EmpName: '',
    },
    employeeList: [],
      partnerList: [],
      partnerParams: {
        PageSize: 10,
        PartnerName: '',
        PageNo: 1,
        PartnerAttr: 'cust',
      },
      currItem: null,
      assList: [
        {
          title: "全部",
          code: 0,
        },{
          title: "未接单",
          code: 1,
        },{
          title: "未完工上报",
          code: 2,
        },{
          title: "已完工上报",
          code: 3,
        },
      ],
      statusList: [
        {
          title: "全部",
          code: 0,
        },
        {
          title: "待安排",
          code: 2,
        },
        {
          title: "服务中",
          code: 3,
        },
        {
          title: "退款中",
          code: 4,
        },
        {
          title: "待审核",
          code: 5,
        },
        {
          title: "待回访",
          code: 6,
        },
        {
          title: "交易关闭",
          code: 8,
        },{
          title: "交易完成",
          code: 9,
        },{
          title: "待结算",
          code: 10,
        },{
          title: "已结算",
          code: 11,
        },
      ],
      kw: "",
      currPageNo: 1,
      totalPagesNo: 1,
      orderList: [],
      AssConfirmDescr: "",
      Latitude: "22",
      Longitude: "11",
      repairCatg: [],
      repairType: [],

      queryForm: {
        DateType: "",
        Start: "",
        End: "",
        CustId: "",
        CustName: "",
        RepairType: "",
        RepairCatg: "",
        Engineer: "",
        EvalStatus: "",
        CancelStatus: 2,
        AuditStatus: "",
        VistStatus: "",
        BusiStatus: "",
        AssStatus: 0,
        CostStatus: "",
        Status: 5,
        Kw: "",
        PageNo: 1,
        PageSize: 10,
      },
    };
  },
  onShow() {
  },
  onLoad() {
    this.initQuery();
    this.fetchData();

    this.fetchEmployeeList()

  },
  methods: {
    handleEmployeeChange(){
      this.showSalesman = false
      this.handleSearch()
    },
    handleEmployeeSearch() {
      this.employeeParams.PageNo = 1
      this.employeeList = []
      this.fetchEmployeeList()
  },
  scrolltoEmployeeLower() {
      this.employeeParams.PageNo = this.employeeParams.PageNo + 1
      this.fetchEmployeeList()

  },
  async fetchEmployeeList() {
      const res = await getEmployeeList(this.employeeParams)
      if (res.MsgCode != 0) return
      console.log('fetchEmployeeList', res)

      this.employeeList = [...this.employeeList, ...res.data.EmployeeList]
  },

    handlePickerConfirm(e) {
      console.log("handlePickerConfirm", e);
      this.currStatus = e.indexs[0];
      this.showPicker = false;
      this.initQuery();
      this.fetchData();
    },
    handleSearch() {
      this.initQuery();
      this.fetchData();
    },
    initQuery() {
      this.queryForm.PageNo = 1;
      this.totalPagesNo = 1;
      this.orderList = [];
    },
    async fetchData() {
      const queryFormCopy = deepClone(this.queryForm)

      queryFormCopy.Engineer = this.employeeList.find(o => o.EmpName === queryFormCopy.Engineer)?.EmpId || 0

      console.log('fetchData form-----', queryFormCopy)
      const res = await getWorkOrderList(queryFormCopy);
      if (res.MsgCode != 0) return;

      this.orderList = [...this.orderList, 
        ...res.data.OrderList.map(item=>{
        item.Buttons = JSON.parse(item.Buttons ? item.Buttons : '{}')
        return item
      })
      ];
      console.log('this.orderList',this.orderList)
      this.totalPagesNo = getTotalPageNum(res.data.Count, 10);
    },
    handleInfoClick(item, type) {
      console.log("handleInfoClick", item);
      if (type == "tel") {
        uni.makePhoneCall({
          phoneNumber: item.RepairInfo.Tel,
        });
        return;
      } else {
        return;
      }
    },
    formatImgUrl(arrStr) {
      try {
        const arr = arrStr.split(",");
        if (arr[0] == "") return;
        return arr.map((item) => {
          return "http://img.help10000.com" + item;
        });
      } catch (ex) {
        console.error("formatImgUrl", ex.message);
      }
    },
    scrolltolower() {
      console.log("scrolltolower", this.totalPagesNo, this.queryForm);
      if (this.queryForm.PageNo < this.totalPagesNo) {
        this.queryForm.PageNo += 1;
        this.fetchData();
      }
    },
  },
};
